package com.yuanren.em.entity.dto;

import com.yuanren.em.entity.Order;
import java.math.BigDecimal;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

public class OrderDTO {
    private BigDecimal totalPrice;
    private String linkUser;
    private String linkPhone;
    private String linkAddress;
    private String state;
    private String goods;
    private String cartId; // 前端传递的cartId字符串，用逗号分隔
    private String cartIds; // 兼容另一种字段名

    public OrderDTO() {}

    public OrderDTO(BigDecimal totalPrice, String linkUser, String linkPhone, String linkAddress, String state, String goods, String cartId) {
        this.totalPrice = totalPrice;
        this.linkUser = linkUser;
        this.linkPhone = linkPhone;
        this.linkAddress = linkAddress;
        this.state = state;
        this.goods = goods;
        this.cartId = cartId;
    }

    // 转换为Order对象
    public Order toOrder() {
        Order order = new Order();
        order.setTotalPrice(this.totalPrice);
        order.setLinkUser(this.linkUser);
        order.setLinkPhone(this.linkPhone);
        order.setLinkAddress(this.linkAddress);
        order.setState(this.state);
        order.setGoods(this.goods);
        
        // 将cartId字符串转换为Long数组，支持多种字段名
        String cartIdStr = this.cartId != null ? this.cartId : this.cartIds;
        if (cartIdStr != null && !cartIdStr.trim().isEmpty()) {
            List<Long> cartIds = Arrays.stream(cartIdStr.split(","))
                    .map(String::trim)
                    .filter(s -> !s.isEmpty())
                    .map(Long::parseLong)
                    .collect(Collectors.toList());
            order.setCartIds(cartIds);
        }
        
        return order;
    }

    // Getters and Setters
    public BigDecimal getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(BigDecimal totalPrice) {
        this.totalPrice = totalPrice;
    }

    public String getLinkUser() {
        return linkUser;
    }

    public void setLinkUser(String linkUser) {
        this.linkUser = linkUser;
    }

    public String getLinkPhone() {
        return linkPhone;
    }

    public void setLinkPhone(String linkPhone) {
        this.linkPhone = linkPhone;
    }

    public String getLinkAddress() {
        return linkAddress;
    }

    public void setLinkAddress(String linkAddress) {
        this.linkAddress = linkAddress;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getGoods() {
        return goods;
    }

    public void setGoods(String goods) {
        this.goods = goods;
    }

    public String getCartId() {
        return cartId;
    }

    public void setCartId(String cartId) {
        this.cartId = cartId;
    }

    public String getCartIds() {
        return cartIds;
    }

    public void setCartIds(String cartIds) {
        this.cartIds = cartIds;
    }
} 